$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'lanling/signuppaymentlog/list',
        datatype: "json",
        colModel: [
            {label: '报名学员姓名', name: 'studentName', index: 'student_name', width: 80, align: "center"},
            {
                label: '性别', name: 'sex', index: 'sex', width: 80, align: "center", formatter: function (value) {
                    return value === 1 ?
                        '<span class="label label-info">男</span>' :
                        '<span class="label label-warning">女</span>';
                }
            },
            {label: '身份证', name: 'idcard', index: 'idCard', width: 80, align: "center"},
            {label: '报名学员手机号码', name: 'phone', index: 'phone', width: 80, align: "center"},
            {label: '公司名称', name: 'companyName', index: 'companyName', width: 80, align: "center"},
            {label: '报名科目名称', name: 'subjectName', index: 'subject_name', width: 80, align: "center"},
            // {label: '报名科目id', name: 'subjectId', index: 'subject_id', width: 80, align: "center"},
            {label: '订单号', name: 'ordercode', index: 'orderCode', width: 80, align: "center"},

            {label: '支付金额', name: 'paymoney', index: 'payMoney', width: 80, align: "center"},
            {
                label: '订单状态', name: 'state', index: 'state', width: 80, align: "center", formatter: function (value) {

                    if (value === 1) {
                        return '<span class="label label-info">未付款</span>';
                    } else if (value === 2) {
                        return '<span class="label label-success">已付款</span>';
                    } else if (value === 3) {
                        return '<span class="label label-warning">已过期</span>';
                    } else if (value === 4) {
                        return '<span class="label label-primary">已线下退款</span>';
                    } else {
                        return '<span class="label label-danger">未知</span>';
                    }
                }
            },
            {label: '订单完成时间', name: 'completetime', index: 'completeTime', width: 80, align: "center"},
            {
                label: '发票处理情况',
                name: 'handle',
                index: 'handle',
                width: 80,
                align: "center",
                formatter: function (value) {
                    return value === 1 ?
                        '<span class="label label-success">已处理</span>' :
                        '<span class="label label-danger">未处理</span>';
                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        signUpPaymentLog: {},
        ruleValidate: {
            name: [
                {required: true, message: 'name不能为空', trigger: 'blur'},
                {max: 100, message: '请输入最多100位'}
            ]
        },
        q: {
            name: ''
        },
        subjectList: [],
        studentList: [],
        modalInvoice: false

    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.signUpPaymentLog = {
                state: '1',
                handle: '1',
                sex: '1',
                invoiceForm: '1',
                invoiceType: '1'
            };
            vm.subjectList = [];
            vm.getSignUpSubjectList();
            vm.getStudentList();
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getSignUpSubjectList();
            vm.getStudentList();
            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.signUpPaymentLog.id == null ? "lanling/signuppaymentlog/save" : "lanling/signuppaymentlog/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(vm.signUpPaymentLog),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "lanling/signuppaymentlog/delete",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "lanling/signuppaymentlog/info/" + id, function (r) {
                vm.signUpPaymentLog = r.signUpPaymentLog;
                vm.signUpPaymentLog.sex = vm.signUpPaymentLog.sex + '';
                vm.signUpPaymentLog.state = vm.signUpPaymentLog.state + '';
                vm.signUpPaymentLog.handle = vm.signUpPaymentLog.handle + '';
                vm.signUpPaymentLog.invoiceForm = vm.signUpPaymentLog.invoiceForm + '';
                vm.signUpPaymentLog.invoiceType = vm.signUpPaymentLog.invoiceType + '';
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getSignUpSubjectList: function () {
            $.get("../../lanling/signupsubject/queryList", function (r) {
                vm.subjectList = r.list;
            });
        },
        getStudentList: function () {
            $.get("../../lanling/student/queryList", function (r) {
                vm.studentList = r.list;
            });
        },
        studentIdChange: function (val) {
            vm.signUpPaymentLog.studentName = val.label.trim();
        },
        signUpSubjectIdChange: function (val) {
            vm.signUpPaymentLog.subjectName = val.label.trim();
        },
        invoiceTypeChange: function () {
            let invoiceType = vm.signUpPaymentLog.invoiceType;
            if (invoiceType === 2 || invoiceType === '2') {
                vm.modalInvoice = true;
            } else {
                vm.modalInvoice = false;
            }
        },
        handleInvoice: function () {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            confirm('确定要处理选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "lanling/signuppaymentlog/handleInvoice",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        }
    }
});